library(tidyverse)
library(cregg) 
library(gtools)
library(scales)

# Note: at the time of publication, cregg was not available for download via CRAN.
# To install cregg, use: remotes::install_github("leeper/cregg") 

###SET WORKING DIRECTORY and LOAD FILES ----

full <- read.csv("b_data/1_raw_data_for_long.csv")


##### Recoding key analytic variables----


full <- full %>%
  mutate(#Race of respondent
        race = 
           case_when(race == 1 ~ 'White respondent',
                     race == 2 ~  'Black respondent'),
         race = factor(race, levels = c("White respondent", "Black respondent")),
         #American identity importance
         amerimport = 
           case_when(Q7_2 == 1 ~ 1,
                     Q7_2 == 2 ~ 2,
                     Q7_2 == 3 ~ 3,
                     Q7_2 == 4 ~ 4),
        #Racial identity importance
         raceimport = 
           case_when(Q7_3 == 1 ~ 1,
                     Q7_3 == 2 ~ 2,
                     Q7_3 == 3 ~ 3,
                     Q7_3 == 4 ~ 4),
         #Perceived prototypicality
         prototypical = 
           case_when(Q7_4 == 1 ~ 1,
                     Q7_4 == 2 ~ 2,
                     Q7_4 == 3 ~ 3,
                     Q7_4 == 4 ~ 4))


###Splitting identity perception variables into two and recoding the variables into high and low ----
full$amerimport_high <- quantcut(full$amerimport, q=2)
full$raceimport_high <- quantcut(full$raceimport, q=2)
full$prototypical_high <- quantcut(full$prototypical, q=2)


full <- full %>%
  mutate(amerimport_high =
           case_when(amerimport_high == "[1,3]" ~ 'Low',
                     amerimport_high == "(3,4]" ~ 'High'),
         raceimport_high =
           case_when(raceimport_high == "[1,3]" ~ 'Low',
                     raceimport_high == "(3,4]" ~ 'High'),
         prototypical_high =
           case_when(prototypical_high == "[1,3]" ~ 'Low',
                     prototypical_high == "(3,4]" ~ 'High'))

####Reshaping data into long form ----

full <- full %>%
  rename (choice_a = Q3_2,
          choice_b = Q3_5,
          choice_c = Q3_8,
          choice_d = Q3_11,
          choice_e = Q3_14,
          rating_a1 = Q3_3_1,
          rating_a2 = Q3_3_2,
          rating_b1 = Q3_6_1,
          rating_b2 = Q3_6_2,
          rating_c1 = Q3_9_1,
          rating_c2 = Q3_9_2,
          rating_d1 = Q3_12_1,
          rating_d2 = Q3_12_2,
          rating_e1 = Q3_15_1,
          rating_e2 = Q3_15_2)


list1 <- list(
  Faces = list(
    ~ Q3A_1_Faces + Q3B_1_Faces + Q3C_1_Faces + Q3D_1_Faces + Q3E_1_Faces,
    ~ Q3A_2_Faces + Q3B_2_Faces + Q3C_2_Faces + Q3D_2_Faces + Q3E_2_Faces
  ),
  Background = list(
    ~ Q3A_1_Background + Q3B_1_Background + Q3C_1_Background + Q3D_1_Background + Q3E_1_Background,
    ~ Q3A_2_Background + Q3B_2_Background + Q3C_2_Background + Q3D_2_Background + Q3E_2_Background
  ),
  Identifies = list(
    ~ Q3A_1_Identifies + Q3B_1_Identifies + Q3C_1_Identifies + Q3D_1_Identifies + Q3E_1_Identifies,
    ~ Q3A_2_Identifies + Q3B_2_Identifies + Q3C_2_Identifies + Q3D_2_Identifies + Q3E_2_Identifies
  ),
  Language = list(
    ~ Q3A_1_Language + Q3B_1_Language + Q3C_1_Language + Q3D_1_Language + Q3E_1_Language,
    ~ Q3A_2_Language + Q3B_2_Language + Q3C_2_Language + Q3D_2_Language + Q3E_2_Language
  ),
  Education = list(
    ~ Q3A_1_Education + Q3B_1_Education + Q3C_1_Education + Q3D_1_Education + Q3E_1_Education,
    ~ Q3A_2_Education + Q3B_2_Education + Q3C_2_Education + Q3D_2_Education + Q3E_2_Education
  ),
  Profession = list(
    ~ Q3A_1_Profession + Q3B_1_Profession + Q3C_1_Profession + Q3D_1_Profession + Q3E_1_Profession,
    ~ Q3A_2_Profession + Q3B_2_Profession + Q3C_2_Profession + Q3D_2_Profession + Q3E_2_Profession
  ),
  Religion = list(
    ~ Q3A_1_Religion + Q3B_1_Religion + Q3C_1_Religion + Q3D_1_Religion + Q3E_1_Religion,
    ~ Q3A_2_Religion + Q3B_2_Religion + Q3C_2_Religion + Q3D_2_Religion + Q3E_2_Religion
  ),
  Political = list(
    ~ Q3A_1_Political + Q3B_1_Political + Q3C_1_Political + Q3D_1_Political + Q3E_1_Political,
    ~ Q3A_2_Political + Q3B_2_Political + Q3C_2_Political + Q3D_2_Political + Q3E_2_Political
  ),
  Spouse = list(
    ~ Q3A_1_Spouse + Q3B_1_Spouse + Q3C_1_Spouse + Q3D_1_Spouse + Q3E_1_Spouse,
    ~ Q3A_2_Spouse + Q3B_2_Spouse + Q3C_2_Spouse + Q3D_2_Spouse + Q3E_2_Spouse
  ),
  rating = list(
    ~ rating_a1 + rating_b1 + rating_c1 + rating_d1 + rating_e1,
    ~ rating_a2 + rating_b2 + rating_c2 + rating_d2 + rating_e2
  )
)

list2 <- list(choice = ~ choice_a + choice_b + choice_c + choice_d + choice_e)

str(long <- cregg::cj_tidy(full,
                    profile_variables = list1,
                    task_variables = list2,
                    id = ~ caseid))
stopifnot(nrow(long) == nrow(full)*5*2) 


#### Writing long data ----

write.csv(long, "b_data/2_long_raw.csv", row.names = FALSE)

### Clear environment ----
rm(list = ls())
